﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sunboxsoft.ChanceData.ServiceProxy.DesignServiceRef;

namespace CPPEI.Coolzon.UI.XProperty.Editors
{
    /// <summary>
    /// Interaction logic for DataSourceEditWindow.xaml
    /// </summary>
    public partial class DataSourceEditWindow : Window
    {
        public DataSource RetDataSource { get; private set; }
        private DataSource _currentDataSource;
        private PublishedDataSource _selectedPDataSource;
        private PublishedDataSet _selectedPDataSet;
        public DataSourceEditWindow(DataSource dataSource)
        {
            InitializeComponent();
            _currentDataSource = dataSource;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (_currentDataSource != null)
            {
                InitZeroView();
            }
            else
            {
                InitFirstView();
            }
        }


        #region 总览-0
        private void InitZeroView()
        {
            if (_currentDataSource.IsStatic)
            {
                _ctlDataSourceType_Pandect.Text = "静态数据源";
                _ctlDataSource_Pandect.Text = _currentDataSource.Name;
                _ctlDataSetTB_Pandect.Visibility = Visibility.Collapsed;
                _ctlDataSet_Pandect.Visibility = Visibility.Collapsed;
                _ctlParams_Pandect.Visibility = Visibility.Collapsed;
                _ctlParamsList_Pandect.Visibility = Visibility.Collapsed;
            }
            else
            {
                _ctlDataSourceType_Pandect.Text = "在线数据源";
                _ctlDataSource_Pandect.Text = _currentDataSource.Name;
                _ctlDataSetTB_Pandect.Visibility = Visibility.Visible;
                _ctlDataSet_Pandect.Visibility = Visibility.Visible;
                _ctlParams_Pandect.Visibility = Visibility.Visible;
                _ctlParamsList_Pandect.Visibility = Visibility.Visible;
                if (_currentDataSource.DataSet != null)
                {
                    _ctlDataSet_Pandect.Text = _currentDataSource.DataSet.Name;
                    if (_currentDataSource.DataSet.Parameters != null && _currentDataSource.DataSet.Parameters.Count > 0)
                    {
                        int row = 0;
                        foreach (var item in _currentDataSource.DataSet.Parameters)
                        {
                            _ctlParamsList_Pandect.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
                            TextBlock tb = new TextBlock();
                            tb.Text = item.Name + ": " + item.ValueString;
                            tb.ToolTip = item.Type;
                            tb.Margin = new Thickness(3);
                            Grid.SetColumn(tb, 0);
                            Grid.SetRow(tb, row);
                            _ctlParamsList_Pandect.Children.Add(tb);
                            row++;
                        }
                    }
                }
            }
        }
        private void _alter_Click(object sender, RoutedEventArgs e)
        {
            InitFirstView();
        }
        private void _button_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }
        #endregion

        #region 数据源类型选择-1
        private void InitFirstView()
        {
            _ctlGroupBoxZero.Visibility = Visibility.Collapsed;
            _ctlGroupBoxFirst.Visibility = Visibility.Visible;
            if (_currentDataSource == null)
            {
                _ctlPreInFirst.Visibility = Visibility.Collapsed;
            }
            else
            {
                _ctlPreInFirst.Visibility = Visibility.Visible;
            }
        }

        private void _preInFirst_Click(object sender, RoutedEventArgs e)
        {
            _ctlGroupBoxZero.Visibility = Visibility.Visible;
            _ctlGroupBoxFirst.Visibility = Visibility.Collapsed;
        }

        private void _nextInFirst_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlStaticRB.IsChecked.HasValue && _ctlStaticRB.IsChecked.Value)
            {
                InitStaticDataSourceView();
            }
            else if (_ctlOnlineRB.IsChecked.HasValue && _ctlOnlineRB.IsChecked.Value)
            {
                InitSecondView();
            }
            else
            {
                CustomMessageBox.ShowWarning("请选择数据源类型", "提示");
            }
        }
        #endregion

        #region 数据源选择-2
        #region 静态数据源
        private void InitStaticDataSourceView()
        {
            _ctlGroupBoxFirst.Visibility = Visibility.Collapsed;
            _ctlGroupBoxStaticDataSource.Visibility = Visibility.Visible;

            Metadata.StaticDataSource[] staticdataSources = Coolzon.Designtime.ResPoolFactory.GetDataSourcePool().GetStaticDataSources();
            if (staticdataSources != null)
            {
                _ctlStaticDataGrid.ItemsSource = staticdataSources;
            }
            _ctlStaticDataSourceFilter.Text = "";
            SetStaticDataSourceFilter();
        }
        private void _ctlStaticDataSourceFilter_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SetStaticDataSourceFilter();
            }
        }
        private void _filterInStatic_Click(object sender, RoutedEventArgs e)
        {
            SetStaticDataSourceFilter();
        }
        private void SetStaticDataSourceFilter()
        {
            if (!string.IsNullOrWhiteSpace(_ctlStaticDataSourceFilter.Text))
            {
                string filter = _ctlStaticDataSourceFilter.Text;
                System.ComponentModel.ICollectionView view = CollectionViewSource.GetDefaultView(_ctlStaticDataGrid.ItemsSource);
                view.Filter = (item) =>
                {
                    return (item as Metadata.StaticDataSource).Name.Contains(filter);
                };
            }
            else
            {
                System.ComponentModel.ICollectionView view = CollectionViewSource.GetDefaultView(_ctlStaticDataGrid.ItemsSource);
                view.Filter = null;
            }
        }
        private void _preInStatic_Click(object sender, RoutedEventArgs e)
        {
            _ctlGroupBoxFirst.Visibility = Visibility.Visible;
            _ctlGroupBoxStaticDataSource.Visibility = Visibility.Collapsed;
        }
        private void _nextInStatic_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlStaticDataGrid.SelectedItem != null)
            {
                RetDataSource = new DataSource();
                RetDataSource.Name = (_ctlStaticDataGrid.SelectedItem as Metadata.StaticDataSource).Name;
                RetDataSource.IsStatic = true;

                InitConfirmView(RetDataSource);
            }
            else
            {
                CustomMessageBox.ShowWarning("请选择静态数据源", "提示");
            }
        }
        #endregion

        #region 在线数据源
        private void InitSecondView()
        {
            _ctlGroupBoxFirst.Visibility = Visibility.Collapsed;
            _ctlGroupBoxSecond.Visibility = Visibility.Visible;

            PublishedDataSource[] dataSources = Coolzon.Designtime.ResPoolFactory.GetDataSourcePool().GetDataSources();
            if (dataSources != null)
            {
                _ctlDataSource.ItemsSource = dataSources;
            }
            _ctlDataSourceFilter.Text = "";
            SetDataSourceFilter();
        }
        private void _filterInSecond_Click(object sender, RoutedEventArgs e)
        {
            SetDataSourceFilter();
        }
        private void _ctlDataSourceFilter_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SetDataSourceFilter();
            }
        }
        private void SetDataSourceFilter()
        {
            if (!string.IsNullOrWhiteSpace(_ctlDataSourceFilter.Text))
            {
                string filter = _ctlDataSourceFilter.Text;
                System.ComponentModel.ICollectionView view = CollectionViewSource.GetDefaultView(_ctlDataSource.ItemsSource);
                view.Filter = (item) =>
                {
                    return (item as PublishedDataSource).Name.Contains(filter);
                };
            }
            else
            {
                System.ComponentModel.ICollectionView view = CollectionViewSource.GetDefaultView(_ctlDataSource.ItemsSource);
                view.Filter = null;
            }
        }
        private void _preInSecond_Click(object sender, RoutedEventArgs e)
        {
            _ctlGroupBoxFirst.Visibility = Visibility.Visible;
            _ctlGroupBoxSecond.Visibility = Visibility.Collapsed;
        }
        private void _nextInSecond_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlDataSource.SelectedItem != null)
            {
                InitDataSetView(_ctlDataSource.SelectedItem as PublishedDataSource);
            }
            else
            {
                CustomMessageBox.ShowWarning("请选择数据源", "提示");
            }
        }
        #endregion
        #endregion

        #region 数据集选择-3
        private void InitDataSetView(PublishedDataSource publishedDataSource)
        {
            _ctlGroupBoxSecond.Visibility = Visibility.Collapsed;
            _ctlGroupBoxThird.Visibility = Visibility.Visible;

            _selectedPDataSource = publishedDataSource;

            _ctlDataSet.ItemsSource = publishedDataSource.DataSet;
        }
        private void _filterInThird_Click(object sender, RoutedEventArgs e)
        {
            SetDataSetFilter();
        }
        private void _ctlDataSetFilter_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SetDataSetFilter();
            }
        }
        private void SetDataSetFilter()
        {
            if (!string.IsNullOrWhiteSpace(_ctlDataSetFilter.Text))
            {
                string filter = _ctlDataSetFilter.Text;
                System.ComponentModel.ICollectionView view = CollectionViewSource.GetDefaultView(_ctlDataSet.ItemsSource);
                view.Filter = (item) =>
                {
                    return (item as PublishedDataSet).Name.Contains(filter);
                };
            }
            else
            {
                System.ComponentModel.ICollectionView view = CollectionViewSource.GetDefaultView(_ctlDataSet.ItemsSource);
                view.Filter = null;
            }
        }
        private void _preInThird_Click(object sender, RoutedEventArgs e)
        {
            _ctlGroupBoxSecond.Visibility = Visibility.Visible;
            _ctlGroupBoxThird.Visibility = Visibility.Collapsed;
        }
        private void _nextInThird_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlDataSet.SelectedItem != null)
            {
                InitDataParameterView(_ctlDataSet.SelectedItem as PublishedDataSet);
            }
            else
            {
                CustomMessageBox.ShowWarning("请选择数据集", "提示");
            }
        }
        #endregion

        #region 参数设置-4
        private void InitDataParameterView(PublishedDataSet publishedDataSet)
        {
            _ctlGroupBoxThird.Visibility = Visibility.Collapsed;
            _ctlGroupBoxForth.Visibility = Visibility.Visible;

            _selectedPDataSet = publishedDataSet;
            
            _ctlParams.Children.Clear();
            _ctlParams.RowDefinitions.Clear();
            if (publishedDataSet.Parameters != null && publishedDataSet.Parameters.Length > 0)
            {
                int row = 0;
                foreach (var item in publishedDataSet.Parameters)
                {
                    _ctlParams.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
                    TextBlock tb = new TextBlock();
                    tb.Text = item.Name;
                    tb.ToolTip = item.Caption;
                    tb.Margin = new Thickness(3, 5, 3, 3);
                    TextBox tbox = new TextBox();
                    tbox.Tag = item;
                    tbox.Margin = new Thickness(3);
                    Grid.SetColumn(tb, 0);
                    Grid.SetColumn(tbox, 1);
                    Grid.SetRow(tb, row);
                    Grid.SetRow(tbox, row);
                    _ctlParams.Children.Add(tb);
                    _ctlParams.Children.Add(tbox);
                    row++;
                }
            }
        }
        private void _preInForth_Click(object sender, RoutedEventArgs e)
        {
            _ctlGroupBoxThird.Visibility = Visibility.Visible;
            _ctlGroupBoxForth.Visibility = Visibility.Collapsed;
        }
        private void _nextInForth_Click(object sender, RoutedEventArgs e)
        {
            RetDataSource = new DataSource();
            RetDataSource.Name = _selectedPDataSource.Name;
            DataSet dataSet = new DataSet();
            dataSet.Name = _selectedPDataSet.Name;
            dataSet.Parameters = GetParameters();
            RetDataSource.DataSet = dataSet;
            
            InitConfirmView(RetDataSource);
        }
        private List<DataParameter> GetParameters()
        {
            List<DataParameter> dataParameterList = new List<DataParameter>();
            if (_ctlParams.Children != null && _ctlParams.Children.Count > 0)
            {
                foreach (var item in _ctlParams.Children)
                {
                    if (item is TextBox)
                    {
                        TextBox tbox = item as TextBox;
                        PublishedDataParameter publishedDataParameter = tbox.Tag as PublishedDataParameter;

                        DataParameter dataParameter = new DataParameter();
                        dataParameter.Name = publishedDataParameter.Name;
                        dataParameter.Type = publishedDataParameter.Type;
                        dataParameter.ValueString = tbox.Text;

                        dataParameterList.Add(dataParameter);
                    }
                }
            }
            return dataParameterList;
        }
        #endregion

        #region 确认页面-5
        private void InitConfirmView(DataSource retDataSource)
        {
            _ctlGroupBoxStaticDataSource.Visibility = Visibility.Collapsed;
            _ctlGroupBoxForth.Visibility = Visibility.Collapsed;
            _ctlGroupBoxFifth.Visibility = Visibility.Visible;
            
            if (retDataSource.IsStatic)
            {
                _ctlDataSourceTypeTB_Confirm.Text = "静态数据源";
                _ctlDataSourceTB_Confirm.Text = retDataSource.Name;
                _ctlDataSetTB_Confirm.Visibility = Visibility.Collapsed;
                _ctlDataSet_Confirm.Visibility = Visibility.Collapsed;
                _ctlParamsListTB_Confirm.Visibility = Visibility.Collapsed;
                _ctlParamsList_Confirm.Visibility = Visibility.Collapsed;
            }
            else
            {
                _ctlDataSourceTypeTB_Confirm.Text = "在线数据源";
                _ctlDataSourceTB_Confirm.Text = retDataSource.Name;
                _ctlDataSetTB_Confirm.Visibility = Visibility.Visible;
                _ctlDataSet_Confirm.Visibility = Visibility.Visible;
                _ctlParamsListTB_Confirm.Visibility = Visibility.Visible;
                _ctlParamsList_Confirm.Visibility = Visibility.Visible;
                if (retDataSource.DataSet != null)
                {
                    _ctlDataSet_Confirm.Text = retDataSource.DataSet.Name;
                    if (retDataSource.DataSet.Parameters != null && retDataSource.DataSet.Parameters.Count > 0)
                    {
                        int row = 0;
                        foreach (var item in retDataSource.DataSet.Parameters)
                        {
                            _ctlParamsList_Confirm.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
                            TextBlock tb = new TextBlock();
                            tb.Text = item.Name + ": " + item.ValueString;
                            tb.ToolTip = item.Type;
                            tb.Margin = new Thickness(3);
                            Grid.SetColumn(tb, 0);
                            Grid.SetRow(tb, row);
                            _ctlParamsList_Confirm.Children.Add(tb);
                            row++;
                        }
                    }
                }
            }
        }
        private void _preInFifth_Click(object sender, RoutedEventArgs e)
        {
            if (RetDataSource.IsStatic)
            {
                _ctlGroupBoxStaticDataSource.Visibility = Visibility.Visible;
            }
            else
            {
                _ctlGroupBoxForth.Visibility = Visibility.Visible;
            }
            _ctlGroupBoxFifth.Visibility = Visibility.Collapsed;
        }
        private void _nextInFifth_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }
        #endregion
    }
}
